package org.halo.trade.data.http;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;

public class BytesResponseHandler implements ResponseHandler<byte[]>
{
	@Override
	public byte[] handleResponse(final HttpResponse response) throws ClientProtocolException, IOException
	{
		int status = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		byte[] ret=entity != null ? EntityUtils.toByteArray(entity) : null;
		EntityUtils.consume(entity);
		
		if (status >= 200 && status < 300)
		{
			return ret;
			
		} else
		{
			throw new ClientProtocolException("Unexpected response status: " + status);
		}
	}
};